package com.generic.util;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;

import com.generic.module.impl.Member;
import com.opensymphony.xwork2.ActionSupport;

public class MailService extends ActionSupport {
	private static final long serialVersionUID = -1708741374660106396L;
	
	private static MailService instance;
	private String siteUrl;
	private String from;
	private String host;
	private String username;
	private String password;
	private String port;
	private String cc = "";
	private String bcc = "";
	private String attPath = "";
	private String[] fileName;
	private String[] mfname;
	private boolean sendFile = false;
	
	private MailService(){		
		siteUrl = getText("activation.site.url");
		from = getText("mail.from");
		host = getText("mail.host");
		username = getText("mail.username");
		password = getText("mail.password");
		port = getText("mail.port");
	}
	
	public static MailService getInstance() {
		if(instance == null) {
			instance = new MailService();
		}
		return instance;
	}
	
	public void sendActivationMail(Member member) throws AddressException, MessagingException {
		String subject = "Account Activation";
		String encodedAccount = Encoder.encode(member.getEmail());

		StringBuffer encodedParams = new StringBuffer();
		encodedParams.append("?a=").append(encodedAccount);
		encodedParams.append("&k=").append(member.getActivationKey());

		StringBuffer content = new StringBuffer();
		content.append("<html>");
		content.append("Dear ").append(member.getFirstName()).append(",<br/><br/>");
		content.append("Thanks for becoming a member!<br/><br/>");
		content.append("Please click the following link to activate your account:<br>");
		content.append("<a href=").append(siteUrl).append(encodedParams.toString()).append(" target=_blank>").append(siteUrl).append(encodedParams.toString()).append("</a>");
		content.append("<br><br>");
		content.append("If you did not register for an account, please simply ignore this email.<br>");
		content.append("This account will be removed within one week if not being activated.");
		content.append("<br/><br/>");
		content.append("Thanks<br/><br/>");
		content.append("</html>");

		MailSender.sendMail(username, password, port, member.getEmail(), cc, bcc, from, host, subject, content.toString(), attPath, fileName, mfname, sendFile);
	}
}